package com.bjshiwei.zhengu.ui.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.bjshiwei.zhengu.R;
import com.bjshiwei.zhengu.ui.fragment.HomeFragment;
import com.bjshiwei.zhengu.ui.fragment.MarketFragment;
import com.bjshiwei.zhengu.ui.fragment.MineFragment;
import com.bjshiwei.zhengu.ui.fragment.ZGFragment;
import com.bjshiwei.zhengu.utils.ToastUtils;
import com.githang.statusbar.StatusBarCompat;

public class MainActivity extends BaseActivity implements HomeFragment.CallBack{

    private Fragment mCurrentFragment;
    private FragmentManager mManager;
    private FragmentTransaction mTransaction;
    private RadioButton[] mRbs;
    private RadioGroup mRadioGroup;
    private HomeFragment homeFragment;
    private boolean isBack = false;
    private static final int mTextCheckedColor = Color.parseColor("#0079f5");
    private static final int mTextUnCheckedColor = Color.parseColor("#999999");
    private static final int mToobarColor = Color.parseColor("#0079f5");
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0 :
                    isBack = false;
                    break;
            }
            super.handleMessage(msg);
        }
    };





    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        StatusBarCompat.setStatusBarColor(this, mToobarColor, true);
        initView();
        initFragment();
        init();
        setData();
        setListener();
    }

    private void setData() {
        showAndHide(R.id.fl_main_content, HomeFragment.class);
        setTextColor(mTextCheckedColor);
        HomeFragment fragment = (HomeFragment) mCurrentFragment;
        fragment.setCallBack(this);
    }

    private void setListener() {

        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_main_home:
                        showAndHide(R.id.fl_main_content, HomeFragment.class);
                        break;
                    case R.id.rb_main_market:
                        showAndHide(R.id.fl_main_content, MarketFragment.class);
                        break;
                    case R.id.rb_main_zg:
                        showAndHide(R.id.fl_main_content, ZGFragment.class);
                        break;
                    case R.id.rb_main_mine:
                        showAndHide(R.id.fl_main_content, MineFragment.class);
                        break;
                    default:
                        break;
                }
                setTextColor(mTextCheckedColor);
            }
        });





    }

    private void init() {
        mManager = getSupportFragmentManager();
    }

    private void initFragment() {

    }

    private void initView() {
        mRadioGroup = (RadioGroup) findViewById(R.id.rg_main_nav);
        mRbs = new RadioButton[4];
        mRbs[0] = (RadioButton) findViewById(R.id.rb_main_home);
        mRbs[1] = (RadioButton) findViewById(R.id.rb_main_market);
        mRbs[2] = (RadioButton) findViewById(R.id.rb_main_zg);
        mRbs[3] = (RadioButton) findViewById(R.id.rb_main_mine);
    }

    @Override
    protected boolean isSlideBack() {
        return false;
    }

    //添加和移除fragment
    private void showAndHide(int contentId, Class<? extends Fragment> clazz){
        //判断当前的fragment是否和需要替换掉的fragment是否一致
        if (mCurrentFragment != null && mCurrentFragment.getClass().getSimpleName().equals(clazz.getCanonicalName())) {
            return;
        }
        mTransaction = mManager.beginTransaction();
        Fragment fragment = null;
        try {
            Fragment fragmentByTag = mManager.findFragmentByTag(clazz.getSimpleName());
            if(fragmentByTag!=null){
                mTransaction.show(fragmentByTag);
                mTransaction.hide(mCurrentFragment);
                mCurrentFragment = fragmentByTag;
            }else{
                fragment = clazz.newInstance();
                mTransaction.add(contentId,fragment,clazz.getSimpleName());
                if(mCurrentFragment!=null){
                    mTransaction.hide(mCurrentFragment);
                }
                mCurrentFragment = fragment;
            }
            mTransaction.commit();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case  KeyEvent.KEYCODE_BACK:
                onKeyBack();
                return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    protected void onKeyBack(){
        if(!isBack) {
            ToastUtils.showShortToast(getApplicationContext(),"再按一次离开点股直播");
            isBack = true;
            mHandler.sendEmptyMessageDelayed(0,2000);
        }else{
            this.finish();
        }
    };
    public void setTextColor(int textColor) {
        for (int i = 0; i < mRbs.length; i++) {
            if(mRbs[i].isChecked()) {
                mRbs[i].setTextColor(mTextCheckedColor);
            }else {
                mRbs[i].setTextColor(mTextUnCheckedColor);
            }
        }
    }

    public Fragment getFragment(Class<? extends Fragment> clazz){
        if (mCurrentFragment != null && mCurrentFragment.getClass().getSimpleName().equals(clazz.getCanonicalName())) {
            return null;
        }
        Fragment fragmentByTag = mManager.findFragmentByTag(clazz.getSimpleName());
        return fragmentByTag;
    }

    @Override
    public void getStatus(boolean flag) {
        if(flag) {
            mRadioGroup.setVisibility(View.GONE);
        }else{
            mRadioGroup.setVisibility(View.VISIBLE);
        }
    }
}
